package com.surent.view.custom.toast;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hailong.surent.R;
import com.surent.view.custom.toast.ToastManager.CancelAppMsg;

/**
 * 
 * @author whlong 自定义Toast类</br>
 * @date 2014年3月16日 11:59:08
 * 
 */
public class AppToast {
	public static final int LENGTH_SHORT = 2000;
	public static final int LENGTH_LONG = 3500;
	public static final int LENGTH_STICKY = -1;
	public static final int PRIORITY_LOW = Integer.MIN_VALUE;
	public static final int PRIORITY_NORMAL = 0;
	public static final int PRIORITY_HIGH = Integer.MAX_VALUE;
	public static final int INFO = 0;
	public static final int ALERT = 1;
	public static final int CONFIRM = 2;
	public static final int CUSTOM = 3;
	public static final int STICKY = 4;
	public static final Style STYLE_ALERT = new Style(LENGTH_LONG, R.color.alert);
	public static final Style STYLE_CONFIRM = new Style(LENGTH_SHORT, R.color.confirm);
	public static final Style STYLE_INFO = new Style(LENGTH_SHORT, R.color.info);

	private final Activity mActivity;
	private int mDuration = LENGTH_SHORT;
	private View mView;
	private ViewGroup mParent;
	private LayoutParams mLayoutParams;
	private boolean mFloating;
	Animation mInAnimation, mOutAnimation;
	int mPriority = PRIORITY_NORMAL;

	public AppToast(Activity activity) {
		mActivity = activity;
	}

	/**
	 * AppToast显示 对象 暂时是动画写死的情况下
	 * 
	 * @author whlong
	 * @param activity
	 *            传入activity实例
	 * @param text
	 *            要显示的文本
	 * @param styleSelected
	 *            显示的样式</br>
	 *            AppToast.INFO:绿色的信息样式，默认为此样式</br>AppToast.ALERT:红色警告样式</br>
	 *            AppToast.
	 *            CONFIRM:黄色强调样式</br>AppToast.CUSTOM:自定义样式，可设置动画</br>AppToast
	 *            .STICKY: STICKY样式，点击关闭才会消失
	 * @param gravity
	 *            Gravity类的参数，设置显示的位置
	 * @return
	 */
	public static void makeText(Activity activity, CharSequence text, int styleSelected) {
		final AppToast.Style style;
		AppToast provided = null;
		switch (styleSelected) {
		case AppToast.ALERT:
			style = AppToast.STYLE_ALERT;
			break;
		case AppToast.CONFIRM:
			style = AppToast.STYLE_CONFIRM;
			break;
		case AppToast.CUSTOM:
			style = new AppToast.Style(LENGTH_SHORT, R.color.custom);
			break;
		case AppToast.STICKY:
			style = new AppToast.Style(LENGTH_STICKY, R.color.sticky);
			provided = AppToast.makeText(activity, text, style, R.layout.apptoast_sticky);
			provided.getView().findViewById(R.id.remove_btn).setOnClickListener(new CancelAppMsg(provided));
			break;
		case AppToast.INFO:
			style = AppToast.STYLE_INFO;
			break;
		default:
			style = AppToast.STYLE_INFO;
			break;
		}

		AppToast appMsg = provided != null ? provided : AppToast.makeText(activity, text, style);
		// 动画
		TranslateAnimation animIn = new TranslateAnimation(0, 0, -100, 0);
		animIn.setDuration(400);
		animIn.setFillAfter(false);
		TranslateAnimation animOut = new TranslateAnimation(0, 0, 0, -100);
		animOut.setDuration(400);
		animOut.setFillAfter(false);

		appMsg.setAnimation(animIn, animOut);

		appMsg.setLayoutGravity(Gravity.TOP);
		appMsg.show();
	}

	public static AppToast makeText(Activity context, CharSequence text, Style style) {
		return makeText(context, text, style, R.layout.apptoast_msg);
	}

	public static AppToast makeText(Activity context, CharSequence text, Style style, float textSize) {
		return makeText(context, text, style, R.layout.apptoast_msg, textSize);
	}

	public static AppToast makeText(Activity context, CharSequence text, Style style, int layoutId) {
		LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflate.inflate(layoutId, null);

		return makeText(context, text, style, v, true);
	}

	public static AppToast makeText(Activity context, CharSequence text, Style style, int layoutId, float textSize) {
		LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflate.inflate(layoutId, null);

		return makeText(context, text, style, v, true, textSize);
	}

	public static AppToast makeText(Activity context, CharSequence text, Style style, View customView) {
		return makeText(context, text, style, customView, false);
	}

	private static AppToast makeText(Activity context, CharSequence text, Style style, View view, boolean floating) {
		return makeText(context, text, style, view, floating, 0);
	}

	private static AppToast makeText(Activity context, CharSequence text, Style style, View view, boolean floating,
			float textSize) {
		AppToast result = new AppToast(context);

		view.setBackgroundResource(style.background);

		TextView tv = (TextView) view.findViewById(android.R.id.message);
		if (textSize > 0)
			tv.setTextSize(textSize);
		tv.setText(text);

		result.mView = view;
		result.mDuration = style.duration;
		result.mFloating = floating;

		return result;
	}

	public static AppToast makeText(Activity context, int resId, Style style, View customView, boolean floating) {
		return makeText(context, context.getResources().getText(resId), style, customView, floating);
	}

	public static AppToast makeText(Activity context, int resId, Style style) throws Resources.NotFoundException {
		return makeText(context, context.getResources().getText(resId), style);
	}

	public static AppToast makeText(Activity context, int resId, Style style, int layoutId)
			throws Resources.NotFoundException {
		return makeText(context, context.getResources().getText(resId), style, layoutId);
	}

	public void show() {
		ToastManager manager = ToastManager.obtain(mActivity);
		manager.add(this);
	}

	public boolean isShowing() {
		if (mFloating) {
			return mView != null && mView.getParent() != null;
		} else {
			return mView.getVisibility() == View.VISIBLE;
		}
	}

	public void cancel() {
		ToastManager.obtain(mActivity).clearMsg(this);

	}

	public static void cancelAll() {
		ToastManager.clearAll();
	}

	/**
	 * 清除当前界面所有AppToast
	 * 
	 * @param activity
	 *            当前的Activity
	 */
	public static void cancelAll(Activity activity) {
		ToastManager.release(activity);
	}

	public Activity getActivity() {
		return mActivity;
	}

	public void setView(View view) {
		mView = view;
	}

	public View getView() {
		return mView;
	}

	public void setDuration(int duration) {
		mDuration = duration;
	}

	public int getDuration() {
		return mDuration;
	}

	public void setText(int resId) {
		setText(mActivity.getText(resId));
	}

	public void setText(CharSequence s) {
		if (mView == null) {
			throw new RuntimeException("This AppMsg was not created with AppMsg.makeText()");
		}
		TextView tv = (TextView) mView.findViewById(android.R.id.message);
		if (tv == null) {
			throw new RuntimeException("This AppMsg was not created with AppMsg.makeText()");
		}
		tv.setText(s);
	}

	public LayoutParams getLayoutParams() {
		if (mLayoutParams == null) {
			mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}
		return mLayoutParams;
	}

	public AppToast setLayoutParams(LayoutParams layoutParams) {
		mLayoutParams = layoutParams;
		return this;
	}

	public AppToast setLayoutGravity(int gravity) {
		mLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, gravity);
		return this;
	}

	public boolean isFloating() {
		return mFloating;
	}

	public void setFloating(boolean mFloating) {
		this.mFloating = mFloating;
	}

	public AppToast setAnimation(int inAnimation, int outAnimation) {
		return setAnimation(AnimationUtils.loadAnimation(mActivity, inAnimation),
				AnimationUtils.loadAnimation(mActivity, outAnimation));
	}

	public AppToast setAnimation(Animation inAnimation, Animation outAnimation) {
		mInAnimation = inAnimation;
		mOutAnimation = outAnimation;
		return this;
	}

	public int getPriority() {
		return mPriority;
	}

	public void setPriority(int priority) {
		mPriority = priority;
	}

	/**
	 * @return Provided parent to add {@link #getView()} to using
	 *         {@link #getLayoutParams()}.
	 */
	public ViewGroup getParent() {
		return mParent;
	}

	/**
	 * Provide a different parent than Activity decor view
	 * 
	 * @param parent
	 *            Provided parent to add {@link #getView()} to using
	 *            {@link #getLayoutParams()}.
	 * 
	 */
	public void setParent(ViewGroup parent) {
		mParent = parent;
	}

	public void setParent(int parentId) {
		setParent((ViewGroup) mActivity.findViewById(parentId));
	}

	public static class Style {

		private final int duration;
		private final int background;

		public Style(int duration, int resId) {
			this.duration = duration;
			this.background = resId;
		}

		public int getDuration() {
			return duration;
		}

		public int getBackground() {
			return background;
		}

		@Override
		public boolean equals(Object o) {
			if (!(o instanceof AppToast.Style)) {
				return false;
			}
			Style style = (Style) o;
			return style.duration == duration && style.background == background;
		}

	}

}
